package cn.wolfcode.p2p.base.service.impl;

import cn.wolfcode.p2p.Exception.ErrorCode;
import cn.wolfcode.p2p.base.domain.Account;
import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.mapper.UserInfoMapper;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.util.AssertUtil;
import cn.wolfcode.p2p.util.BitStatesUtils;
import cn.wolfcode.p2p.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserInfoServiceImpl implements IUserInfoService {
    @Autowired
    public UserInfoMapper userInfoMapper;


    @Override
    public void save(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }



    @Override
    public UserInfo getUserInfo(Long id) {
        return userInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateUserInfo(UserInfo userInfo) {
        //验证参数
        //设置参数
        LoginInfo loginInfo = UserContext.getLoginInfo();
        UserInfo temp = userInfoMapper.selectByPrimaryKey(loginInfo.getId());
        if(!userInfo.hasBasicInfo()){
            temp.addBitState(BitStatesUtils.OP_BASIC_INFO);
        }
        temp.setEducationBackground(userInfo.getEducationBackground());
        temp.setIncomeGrade(userInfo.getIncomeGrade());
        temp.setMarriage(userInfo.getMarriage());
        temp.setKidCount(userInfo.getKidCount());
        temp.setHouseCondition(userInfo.getHouseCondition());
        //更新
        update(temp);

    }

    //做锁判断,更新数据
    @Override
    public void update(UserInfo userInfo){
        int count = userInfoMapper.updateByPrimaryKey(userInfo);
        //并发访问失败之后,需要抛出异常,否则本操作不会更新,但其他操作会更新,因为没有出现异常,事务会提交
        AssertUtil.instance().isFalse(count == 0, ErrorCode.INFORMATION_MODIFICATION_ERROR);

    }
}
